package com.atcjx.pojo.Article;


import com.atcjx.MyUtils.BlogUtils;
import com.atcjx.pojo.Article.Enum.ArticleTypeEnum;
import com.atcjx.pojo.IConverter;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName ArticlesVo.java
 * @Description 非DAO层的Article类，传回给前端
 * @createTime 2021年09月20日 15:10:00
 */
@ApiModel("文章类的值对象")
@Data
public class ArticleVO extends baseArticle {
    private Object[] tags;
    private String type;
    private String gmtCreate;
    private String gmtUpdate;
    private String tabloid;
    private Integer views;
    private String status;
    private String reprintURL;
    private String coverImg;
    /**
     * 根据 PO 创建 VO 对象
     * @param article PO对象
     * @return VO对象
     */
    public static ArticleVO fromArticlePO(Article article) {
        return new Converter().convertToVo(article);
    }

    /**
     * 将PO列表转化为VO列表
     * @param articles
     * @return
     */
    public static List<ArticleVO> fromArticlePoLIst(List<Article>articles){return new Converter().convertToVO(articles);}

    public static void from() {
    }

    private static class Converter implements IConverter<Article, ArticleVO> {
        @Override
        public ArticleVO convertToVo(Article article)  {
            final ArticleVO articleVO=new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            List<String> tags1 = article.getArtTags();
            articleVO.setTags(tags1.toArray());
            for(ArticleTypeEnum item:ArticleTypeEnum.values()){
                if(item.getFlag()==article.getType()){
                    articleVO.setType(item.getNotes());
                }
            }
            try {
                articleVO.setGmtCreate(BlogUtils.longToString(article.getGmtCreate()));
                articleVO.setGmtUpdate(BlogUtils.longToString(article.getGmtUpdate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return articleVO;
        }

        @Override
        public List<ArticleVO> convertToVO(List<Article> t) {
            final List<ArticleVO>list=new ArrayList<>();
            for(Article article:t)
                list.add(ArticleVO.fromArticlePO(article));
            return list;
        }

    }
}
